/**
 * Created by godhh on 2016/12/21.
 */
import angular from 'angular';
import 'apiService';
import _ from 'lodash';
import '@yunke/angular-ui/lib/uiRadioArray';
import '@yunke/angular-ui/lib/ykDropdown';
import 'angular-ui';
import 'uploader';

const app = angular.module('stockOutHelper', ['ApiService', 'angular.directive.ykDropdown']);

app
    .filter('authorizationType', function () {
        return function (origin) {
            return {
                '1': '新增',
                '2': '增购',
                '3': '续签',
                '4': '混签'
            }[origin];
        };
    })
    .filter('applyNum', () => (origin, isSp) => {
        return _.filter(origin, x => isSp ? !!x.applyNum : true);
    })
    .directive('ykAsyncRender', [
        '$parse', '$timeout', 'api', function ($parse, $timeout, api) {

            const template = `<div class="table-wrap" style="max-height: 300px;overflow-y: scroll;overflow-x: hidden;">
            <table width="100%">
                <thead ng-if="list.length && showHead">
                <tr>
                    <th ng-repeat="col in cols track by $index" ng-style="col.style || {}">{{col.name}}</th>
                </tr>
                </thead>
                <tbody>
                <tr ng-repeat="item in (list || [])" ng-click="ngClick($event, item, $index)">
                    <td height="30" ng-if="cols.length" ng-repeat="col in cols track by $index">{{item[col.value]}}</td>
                    <td height="30" ng-if="!cols.length">{{item[name]}}</td>
                </tr>
                <tr ng-if="!list.length">
                    <td height="30">暂无数据</td>
                </tr>
                </tbody>
            </table>
        </div>`;

            return {
                scope: {
                    model: '=ykAsyncRenderModel',
                    inputText: '=ykAsyncRenderInputText',
                    cols: '@?ykAsyncRenderCols',
                    name: '@ykAsyncRenderName',
                    value: '@ykAsyncRenderValue',
                    showHead: '@?ykAsyncRenderShowHead',
                    method: '@ykAsyncRenderFetchMethod',
                    keywordName: '@ykAsyncRenderKeywordName',
                    getData: '&?ykAsyncRenderGetData',
                    dataPath: '@ykAsyncRenderDataPath',
                    onClick: '&?ykAsyncRenderOnClick',
                    params: '@ykAsyncRenderParams'
                },
                template,
                link: function (scope) {

                    const render = _.debounce(function (keyword) {

                        const service = api.getService();

                        scope.showHead = scope.showHead || false;

                        if (_.isString(scope.cols)) {
                            scope.cols = $parse(scope.cols)(scope);
                        } else {
                            scope.cols = scope.cols || [];
                        }

                        if (_.isString(scope.params)) {
                            scope.params = $parse(scope.params)(scope);
                        } else {
                            scope.params = scope.params || {};
                        }

                        let render = res => {
                            scope.list = scope.getData ? scope.getData()(res) : (scope.dataPath
                                ? _.get(res, scope.dataPath)
                                : res.data.data);
                        };

                        let promise = service[scope.method](_.merge({}, {[scope.keywordName]: keyword}, scope.params));

                        promise.then(render, render);

                    }, 200);

                    let clicking = false;

                    scope.$watch('inputText', function (newValue, oldValue) {

                        if (!clicking && newValue !== oldValue) {

                            render(newValue);

                        }

                    });

                    scope.ngClick = function ($event, $item, $index) {
                        clicking = true;
                        scope.model = $item[scope.value];
                        scope.inputText = $item[scope.name];
                        if (scope.onClick) {
                            scope.onClick({
                                $event, $item, $index, $scope: scope
                            });
                        }
                        $timeout(() => clicking = false, 0);
                    };

                }
            };

        }])
    .service('mapSnakeCase', function () {

        const mapSnakeCase = xs => {

            const snakeCase = x => {

                const values = _.mapValues(x, (value) => {

                    if (_.isObject(value) || _.isArray(value)) {
                        return mapSnakeCase(value);
                    } else {
                        return value;
                    }

                });

                return _.mapKeys(values, (value, key) => _.snakeCase(key));

            };

            if (_.isArray(xs)) {
                if (_.some(xs, x => !_.isObject(x))) {
                    return xs;
                }
                return _.map(xs, snakeCase);
            } else if (_.isObject(xs)) {
                return snakeCase(xs);
            } else if (_.isString(xs)) {
                return _.snakeCase(xs);
            } else {
                return xs;
            }

        };

        return mapSnakeCase;

    });

export default app;
